package io;

import java.io.*;
import java.util.Scanner;
//复制文件
public class IODemo14 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要复制的源文件:");
        String srcPath = scanner.next();
        System.out.println("请输入要复制的目标文件");
        String destPath = scanner.next();

        //合法性判定
        //1、srcPath对应的文件是否存在
        File srcfile = new File(srcPath);
        if (!srcfile.isFile()){
            System.out.println("源文件路径有误！！！");
            return;
        }
        //2、destPath对应的目录是否存在
        File desfile = new File(destPath);
        if (!desfile.getParentFile().isDirectory()){
            System.out.println("目标路径有误！！！");
            return;
        }
        //创建inputStream和outputStream来读写对象
        try(InputStream inputStream = new FileInputStream(srcPath);
        OutputStream outputStream = new FileOutputStream(destPath)) {
            while(true){
                byte[] buffer = new byte[1024];
               int n = inputStream.read(buffer);
               if(n == -1){
                   break;
               }
               //把读到的内容写入outputStream中
                outputStream.write(buffer,0,n);
            }
        }
    }
}
